﻿using System;
using System.Linq;
using System.Web.Mvc;
using KPIS.GERP.Models.ICM;
using KPIS.GERP.WEB.GIMS.Class;
using KPIS.GERP.WEB.GIMS.MasResourceService;
using Resources;

namespace KPIS.GERP.WEB.GIMS.Controllers
{
    // [AccessDeniedAuthorize]
    public class MasResourceSubGroupController : Controller
    {
        MasResourceServiceClient s = new MasResourceServiceClient();
        string[] _defaultInclude = new[]{"MasResourceGroup"};

        #region resource sub group
        public ActionResult Index(int resGrpId)
        {
            // Resource sub group 
            var model = s.SelectResourceSubGroup(new MasResourceSubGroup{RecordStatus = "!D", ResGrpId = resGrpId}, _defaultInclude);
            ViewBag.resGrpId = resGrpId;

            return View(model); 
        }

		public ActionResult Details(int id)
		{
			var model = s.SelectResourceSubGroup(new MasResourceSubGroup {ResSubGrpId = id}, _defaultInclude).FirstOrDefault();
            if(model == null) return new HttpNotFoundResult("ไม่พบข้อมูล");
		    int resGrpId = model.ResGrpId.Value;
		    ViewBag.resGrpId = resGrpId;

			return View(model);
		}

		public ActionResult Create(int resGrpId)
		{
		    var model = new MasResourceSubGroup();
		    model.ResGrpId = resGrpId;
		    ViewBag.resGrpId = resGrpId;

			return View(model);
		}

		[HttpPost]
		[ValidateAntiForgeryToken]
		public ActionResult Create(MasResourceSubGroup model, int resGrpId)
		{
		    model = VarifyModel(model);
			if (ModelState.IsValid)
			{
				var id = s.InsertResourceSubGroup(model, GimsMembership.CurrentEmpId);
				return RedirectToAction("Details", new {id, resGrpId});
			}

		    ViewBag.resGrpId = resGrpId;

			return View(model);
		}

		public ActionResult Edit(int id, int resGrpId)
		{
			var model = s.SelectResourceSubGroup(new MasResourceSubGroup {ResSubGrpId = id}, _defaultInclude).FirstOrDefault();
		    ViewBag.resGrpId = resGrpId;

			return View(model);
		}

		[HttpPost, ValidateAntiForgeryToken]
		public ActionResult Edit(int id, MasResourceSubGroup model, int resGrpId)
		{
		    model = VarifyModel(model);
		    if (ModelState.IsValid)
		    {
		        s.UpdateResourceSubGroup(model, GimsMembership.CurrentEmpId);
		        return RedirectToAction("Details", new{id, resGrpId});
		    }

		    ViewBag.resGrpId = resGrpId;

		    return View(model);
		}

        public ActionResult Delete(int id, int resGrpId)
        {
            s.DeleteResourceSubGroup(id, GimsMembership.CurrentEmpId);
            return RedirectToAction("Index", new{resGrpId});
        }

        [HttpPost, ValidateAntiForgeryToken]
		public ActionResult DeleteAll(int[] deleteIds)
		{
			if (deleteIds == null)
				return Json(new { success = true });

			try
			{
				foreach (int delete in deleteIds)
				{
					s.DeleteResourceSubGroup(delete, GimsMembership.CurrentEmpId);
				}

				ModelState.AddModelError(ErrorLocalization.Error, 
                    string.Format("ลบข้อมูลไปทั้งหมด {0} รายการ", deleteIds.Length.ToString("#,##0")));
				return Json(new {success = true});
			}
			catch (Exception ex)
			{
				ModelState.AddModelError(ErrorLocalization.Error, ErrorLocalization.ErrorWhileDeleteData);
				Models.Class.GerpLog.Error(ErrorLocalization.ErrorWhileDeleteData + ", " + ex.Message);
				return Json(new {success = false});
			}
		}
        
        private MasResourceSubGroup VarifyModel(MasResourceSubGroup model)
        {
            if (string.IsNullOrWhiteSpace(model.Name))
            {
                ModelState.AddModelError("Name", "กรุณากรอกชื่อกลุ่มพัสดุ");
            }

            if(model.ResGrpId == 0 || model.ResGrpId == null || s.SelectResourceGroup(new MasResourceGroup {ResGrpId = model.ResGrpId.Value, RecordStatus = "!D"}, null).FirstOrDefault() == null)
            {
                ModelState.AddModelError("ResSubGrpId", "ไม่มีข้อมูลหมวดพัสดุ");
            }

            return model;
        }
#endregion

    }
}
